package ru.spbau.design.snmp.android.viewer.view;

import ru.spbau.design.snmp.android.viewer.R;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

class ConfigurationTab {
	ViewerActivity.Accessor accessor;
	NetworkPickerClickListener networkPickerClickListener;
	EditText collectorAddressEdit;
	EditText collectorPortEdit;
	ExpandableListView networkPicker;
	NetworksAdapter adapter;

	public ConfigurationTab(ViewerActivity.Accessor accessor) {
		this.accessor = accessor;

		this.collectorAddressEdit = (EditText) accessor.context().findViewById(
				R.id.collectorAddress);
		this.collectorPortEdit = (EditText) accessor.context().findViewById(
				R.id.collectorPort);
		this.networkPicker = (ExpandableListView) accessor.context()
				.findViewById(R.id.networkPicker);

		this.networkPickerClickListener = new NetworkPickerClickListener();
		this.adapter = new NetworksAdapter();

		this.networkPicker.setAdapter(this.adapter);
		this.networkPicker
				.setOnChildClickListener(this.networkPickerClickListener);

		Button setConfBut = (Button) accessor.context().findViewById(
				R.id.setConfiguration);
		setConfBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				updateConfiguration();
			}
		});
	}

	private void updateConfiguration() {
		this.accessor.context().updateConfiguration(
				collectorAddressEdit.getText().toString(),
				Integer.parseInt(collectorPortEdit.getText().toString()));
	}

	// --------------------------------------------------------------------------------

	private class NetworksAdapter implements ExpandableListAdapter {
		public boolean areAllItemsEnabled() {
			return true;
		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isEmpty() {
			if (accessor.networks() == null) {
				return true;
			}

			return accessor.networks().isEmpty();
		}

		public void registerDataSetObserver(DataSetObserver observer) {
		}

		public void unregisterDataSetObserver(DataSetObserver observer) {
		}

		public Object getChild(int arg0, int arg1) {
			return null;
		}

		public long getChildId(int groupPos, int childPos) {
			return childPos + 1;
		}

		public View getChildView(int groupPos, int childPos, boolean arg2,
				View convertView, ViewGroup arg4) {
			TextView tv = (TextView) convertView;
			if (tv == null) {
				tv = new TextView(accessor.context());
			}

			tv.setText(accessor.networks().get(childPos).getId());
			tv.setMinimumHeight(60);

			return tv;
		}

		public int getChildrenCount(int arg0) {
			if (accessor.networks() == null) {
				return 0;
			}
			 
			return accessor.networks().size();
		}

		public long getCombinedChildId(long arg0, long childPos) {
			return childPos + 1;
		}

		public long getCombinedGroupId(long arg0) {
			return 0;
		}

		public Object getGroup(int arg0) {
			return null;
		}

		public int getGroupCount() {
			return 1;
		}

		public long getGroupId(int arg0) {
			return 0;
		}

		public View getGroupView(int arg0, boolean arg1, View convertView,
				ViewGroup arg3) {
			TextView tv = (TextView) convertView;
			if (tv == null) {
				tv = new TextView(accessor.context());
			}

			tv.setText("Networks");
			tv.setMinimumHeight(50);
			return tv;
		}

		public boolean isChildSelectable(int arg0, int arg1) {
			return true;
		}

		public void onGroupCollapsed(int arg0) {
		}

		public void onGroupExpanded(int arg0) {
		}
	}

	// --------------------------------------------------------------------------------

	private class NetworkPickerClickListener implements
			ExpandableListView.OnChildClickListener {
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			accessor.context().updateConfiguration(childPosition);
			networkPicker.invalidateViews();
			return true;
		}
	}
}
